package com.example.shopping.utils;


import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.shopping.R;
import com.example.shopping.me.LoginActivity;
import com.example.shopping.me.PersonCenterActivity;
import com.example.shopping.me.UserBean;

/**
 * A simple {@link Fragment} subclass.
 */
public class MeFragment extends Fragment implements View.OnClickListener{
    Button cartBtn,historyBtn,centerBtn,aboutBtn,settingBtn;
    ImageView headIv;
    TextView nicknameTv;
    private UserBean.DataBean dataBean;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
       View view= inflater.inflate(R.layout.fragment_me2, container, false);
       initview(view);
       return  view;
    }

        public void onResumePage() {
        dataBean = SaveUserUtils.getUserinfo();
        if (dataBean!=null){
            String nickname=dataBean.getNickname();
            nicknameTv.setText(nickname);
        }
        else {
            nicknameTv.setText("请先登录");
        }
    }
    private  void  startCenterPage(){
        if (dataBean==null) {
            Toast.makeText(getContext(),"还未登陆，请先登录账号！",Toast.LENGTH_SHORT).show();;
            Intent it =new Intent(getActivity(), LoginActivity.class);
            startActivity(it);
        }else {
            Intent it =new Intent(getActivity(), PersonCenterActivity.class);
            startActivity(it);
        }
    }

    private void initview(View v) {
        cartBtn = v.findViewById(R.id.frag_me_btn_cart);
        historyBtn = v.findViewById(R.id.frag_me_btn_history);
        centerBtn = v.findViewById(R.id.frag_me_btn_center);
        aboutBtn = v.findViewById(R.id.frag_me_btn_about);
        settingBtn = v.findViewById(R.id.frag_me_btn_setting);
        headIv = v.findViewById(R.id.frag_me_iv);
        nicknameTv = v.findViewById(R.id.frag_me_tv_name);

        cartBtn.setOnClickListener(this);
        historyBtn.setOnClickListener(this);
        aboutBtn.setOnClickListener(this);
        centerBtn.setOnClickListener(this);
        settingBtn.setOnClickListener(this);
        headIv.setOnClickListener(this);
        nicknameTv.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.frag_me_btn_cart:
                break;
            case R.id.frag_me_btn_history:
                break;
            case R.id.frag_me_btn_center:
                startCenterPage();
                break;
            case R.id.frag_me_btn_about:
                break;
            case R.id.frag_me_btn_setting:
                break;
            case R.id.frag_me_iv:
              startCenterPage();
                break;

        }

    }
}
